/* eslint-disable indent */
import request from '@/utils/request'
import { tansParams, blobValidate } from "@/utils/commonSettings";
import { method } from 'lodash'
import proxy from '@/config/host'

export function getListData(params) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/list`,
    method: 'get',
    params: params,
    headers: {
      showLoading: true,
      showUploading: false,
    }
  })
}

export function getListData_fj(params) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/zscqgl/list/fj`,
    method: 'get',
    params: params,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  })
}

export function getSelectorData(name, params) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/selector/` + name,
    method: 'get',
    params: params,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  })
}


export function submit(data) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/submit`,
    method: 'post',
    data: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}

export function submit_delete(data) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/submit/delete`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}

export function submit_delfile_fj(data) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/submit/delfile_fj`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}

export function exportExcel(download, data, filename) {
  download(
    "cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/yqsbjd/export/excel",
    data,
    filename,
  );
}

export function uploadFile(data) {
  return request({
    url:'/file/upload',
    method: "post",
    data,
  });
}

export function downloadFile(data) {
  request({
    url: `cenertech-inner-syfxxt-khfw/pd/yqsbgl/sbjdjz/zscqgl/miniofileinfo`,
    method: 'get',
    params: data,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  }).then((res)=>{
    if (res.rows && res.rows.length > 0) {
      var fileinfo = res.rows[0];
      request({
        url:"/file/download/" + fileinfo.path,
        method: "get",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        responseType: "blob",
        headers: {
          showLoading: false,
          showUploading: false,
        }
      }) .then((filedata) => {
        const isLogin = blobValidate(filedata);
        if (isLogin) {
          var blob = new Blob([filedata]);
          saveAs(blob, fileinfo.filename);
        } else {
          const resText = filedata.text();
          const rspObj = JSON.parse(resText);
          const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode.default;
          Message.error(errMsg);
        }
      }) .catch((r) => {
        Message.error("下载文件出现错误");
      })
    } else {
      Message.error("下载文件出现错误");
    }

  }).catch((r) => {
    Message.error("下载文件出现错误");
  });
  
}
